package com.crm.system.controller.common;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.common.Dictionary;
import com.crm.system.service.common.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author luojie
 * @date 2021/1/19 17:46
 */
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 根据字典code(type_code)查询字典信息
     * @param typeCode ---大分类
     * @return
     */
    @GetMapping("/selectDictionaryByTypeCode")
    public DataResponse selectDictionaryByTypeCode(@RequestParam(value="typeCode")String typeCode){
        try {
            List<Dictionary> dictionaryList = dictionaryService.selectDictionaryByTypeCode(typeCode);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),dictionaryList);
        } catch (Exception e) {
            log.error("dictionary selectDictionaryByTypeCode error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 添加字典信息
     * @param dictionary
     * @return
     */
    @PostMapping("/addDictionary")
    public DataResponse addDictionary(Dictionary dictionary){
        try{
            Boolean result = dictionaryService.insertDictionary(dictionary);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("dictionary addDictionary error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据字典大分类/小分类查询
     * @param typeCode  ---大分类
     * @param dictCode  ---小分类
     * @return
     */
    @PostMapping("/selectDictionaryByCode")
    public DataResponse selectDictionaryByCode(@RequestParam(value="typeCode")String typeCode,@RequestParam(value="dictCode")String dictCode){
        try {
            Dictionary dictionary = dictionaryService.selectDictionaryByOne(typeCode,dictCode);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),dictionary);
        } catch (Exception e) {
            log.error("dictionary selectDictionaryByCode error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }
}
